/**
 * 公共确认提示框插件
 * 定义函数用于构造，来传递参数
 * @author lee
 */
var ShowConfirmDialog = function(opt) {
    //默认配置参数
    this.opt = {
		msg:"这里是确认消息内容",  //提示消息内容
		title:"确认提示框",      //弹出框标题
		icon:"info",          //图标:info-普通提示,error-错误提示,warn-警告提示
		okButton:"确认",       //确认按钮内容
		cancelButton:"取消",   //取消按钮内容
		confirmCallback:null, //确认回调函数
    };
    
    //如果用户输入参数，将参数扩展
    if (opt && $.isPlainObject(opt)) {
        $.extend(this.opt, opt);
    }
    
    //给函数定义变量，并和opt对象一起传入原型
    this.body = $("body");
    this.dialog = $("<div class='modal modal-tahoe dialog-tahoe' tabindex='-1'></div>");
};

//原型中定义函数
ShowConfirmDialog.prototype = {
    init:function() {
        //1.this指的就是该原型对象
        //2.this.xxx表示原型对象调用函数中的变量
        var _this_ = this,
            opt = this.opt,
            body = this.body,
            dialog = this.dialog;
        
        var timestamp = new Date().getTime();
        var dialog_id = "msg_dialog_"+timestamp;
        dialog.attr("id",dialog_id);

        var content = "<div class='modal-dialog modal-sm'>";
        	content +="	 <div class='modal-content'>";
        	content +="	 	<div class='modal-header'>";
        	content +="	 		<button data-id='close' class='close icon icon-close'></button>";
        	content +="	 		<h5 data-id='title' class='modal-title'>";
        	content +="	 		</h5>";
        	content +="	 	</div>";
        	content +="	 	<div class='modal-body tc pt25'>";
//        	content +="	 		<i data-id='icon'></i>";
        	content +="	 		<p data-id='msg' class='inline_block'>";
        	content +="	 		</p>";
        	content +="	 	</div>";
        	content +="	 	<div class='modal-footer tc'>";
        	content +="	 		<button data-id='ok' type='button' class='btn' yesbtn='true'>确定</button>";
        	content +="	 		<button data-id='cancel' class='btn btn-dismiss'  nobtn='true'>取消</button>";
        	content +="	 	</div>";
        	content +="	 </div>";
        	content +="</div>";
        	
        dialog.html(content);
        body.append(dialog); 
        
        var iconEl = dialog.find("i[data-id='icon']");
        
        var okBtn = dialog.find("button[data-id='ok']");
        var cancelBtn = dialog.find("button[data-id='cancel']");
        var closeBtn = dialog.find("button[data-id='close']");
        
        var titleEl = dialog.find("h5[data-id='title']");
        var msgEl = dialog.find("p[data-id='msg']");
        
        //设置图标
        if(opt.icon=="info"){
        	$(iconEl).addClass("glyphicon glyphicon-info-sign");
        }else if(opt.icon=="error"){
        	//TODO:暂无错误图标
        }else if(opt.icon=="warn"){
        	//TODO:暂无警告图标
        }
        
        //设置标题
        if(opt.title){
        	$(titleEl).html(opt.title);
        }
        
        //设置提示内容
        if(opt.msg){
        	$(msgEl).html(opt.msg);
        }
        
        //设置确认按钮
        if(opt.okButton){
        	$(okBtn).text(opt.okButton);
        }
        
        //设置取消按钮
        if(opt.cancelButton){
        	$(cancelBtn).text(opt.cancelButton);
        }
        
        //确认点击事件
        $(okBtn).click(function(event){
			_this_.ok();
		});
        
        //取消点击事件
        $(cancelBtn).click(function(event){
			_this_.cancel();
		});
        
        //关闭点击事件
        $(closeBtn).click(function(event){
			_this_.cancel();
		});
    },
    checkResource:function(){
    },
    //关闭弹出框
    close: function() {
        this.dialog.modal("hide");
        $(this.dialog).remove();
    },
    //显示弹出框
    show:function(){
    	var _this_ = this,
        dialog = this.dialog;
    	
    	_this_.init();
    	dialog.modal("show");
    },
    ok:function() {
    	var _this_ = this,
        opt = this.opt,
        body = this.body,
        dialog = this.dialog;
    	
    	_this_.close();
    	
    	if(opt.confirmCallback){
    		opt.confirmCallback(true);
    	}
    },
    cancel:function() {
    	var _this_ = this,
    	opt = this.opt,
    	body = this.body,
    	dialog = this.dialog;
    	
    	_this_.close();
    	
    	if(opt.confirmCallback){
    		opt.confirmCallback(false);
    	}
    }
};

//以下是使用demo1
function showConfirm1() {
	var dialog = new ShowConfirmDialog();
	dialog.show();
}

//以下是使用demo2
function showConfirm2() {
	var dialog = new ShowConfirmDialog({
		icon:"info",
    	title:"确认框",
		msg:"确定要删除吗?",
		okButton:"ok",
		cancelButton:"cancel"
    });
	dialog.show();
}

//以下是使用demo3
function showConfirm3() {
	var dialog = new ShowConfirmDialog({
		icon:"info",
    	title:"确认框",
		msg:"是否确定要删除?",
		okButton:"是",
		cancelButton:"否",
		confirmCallback:function(isOk){
			 if(isOk){
				 console.log("确认后执行...");
			 }else{
				 console.log("取消后执行...");
			 }
		}
    });
	dialog.show();
}